<?php


namespace app\app\controller;


use think\Db;

class Manager extends WxUser
{

    protected function initialize()
    {
        parent::initialize();

        if ($this->memberInfo['is_manage'] != 1) {
            responseJson(-100, '当前不是管理端');
        }

    }
    //服务订单列表
    //0 全部 1带指派 2已指派 3进行中 4已完成
    //1待支付2带指派3服务中4已完成5已取消6已退款7已指派8已评价'
    public function orderList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['is_del', '=', 0];
        if (isset($data['order_no']) && $data['order_no']) {
            $where[] = ['order_no', 'like', '%' . $data['order_no'] . '%'];
        }
        //服务类型 0全部服务 1抢单服务 2派单服务
        if (isset($data['server_type']) && $data['server_type']) {
            $where[] = ['dispatch_able', '=', $data['server_type']];
        }
        if ($data['status'] == 0) {
            $where[] = ['status', 'in', [2, 3, 4, 7, 8]];
        }
        if ($data['status'] == 1) {
            $where[] = ['status', 'in', [2]];
        }
        if ($data['status'] == 2) {
            $where[] = ['status', 'in', [7]];
        }
        if ($data['status'] == 3) {
            $where[] = ['status', 'in', [3]];
        }
        if ($data['status'] == 4) {
            $where[] = ['status', 'in', [4, 8]];
        }
        $order_list = Db::name('server_order')
            ->where($where)->order('create_time desc')
            ->paginate($length, false, ['page' => $page])
            ->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                if ($item['time']) {
                    $item['time'] = date('Y-m-d H:i:s', $item['time']);
                }
                if ($item['pay_time']) {
                    $item['pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
                }
                if ($item['dispatch_time']) {
                    $item['dispatch_time'] = date('Y-m-d H:i:s', $item['dispatch_time']);
                }
                if ($item['start_time']) {
                    $item['start_time'] = date('Y-m-d H:i:s', $item['start_time']);
                }
                if ($item['end_time']) {
                    $item['end_time'] = date('Y-m-d H:i:s', $item['end_time']);
                }
                $server_info = Db::name('server')->find($item['server_id']);
                $item['server_info'] = $server_info;
                if ($item['pz_id']) {
                    $item['pz_info'] = Db::name('pz')->find($item['pz_id']);
                } else {
                    $item['pz_info'] = [];
                }
                $server_order_img = Db::name('server_order_img')->where([
                    'order_id' => $item['id']
                ])->order('type')->select();
                foreach ($server_order_img as $k => $v) {
                    $server_order_img[$k]['create_time'] = date('Y-m-d H:i:s', $server_order_img[$k]['create_time']);
                    switch ($v['type']) {
                        case 1:
                            $server_order_img[$k]['desc'] = '开始服务';
                            break;
                        case 2:
                            $server_order_img[$k]['desc'] = '服务中';
                            break;
                        case 3:
                            $server_order_img[$k]['desc'] = '服务结束';
                            break;
                        default:
                            $server_order_img[$k]['desc'] = '未知';
                    }
                }
                $item['server_order_img'] = $server_order_img;
                return $item;
            });
        msg(0, '查询成功', $order_list);
    }

    //分配订单
    public function dispatchOrder()
    {
        $data = $this->request->param();
        $order_id = $data['order_id'];
        $pz_id = $data['pz_id'];
        $order_info = Db::name('server_order')->find($order_id);
        $pz_info = Db::name('pz')->find($pz_id);
        if (!$order_info) {
            msg(-1, '订单不存在', '');
        }
        if (!$pz_info) {
            msg(-2, '服务人员不存在', '');
        }
        if (in_array($order_info['status'], [1, 3, 4, 5, 6, 8])) {
            msg(-3, '当前订单不可派遣', '');
        }
        //todo 护士能接陪诊、护士单，陪诊能接护士单
        $update = array(
            'id' => $data['order_id'],
            'status' => 7,
            'pz_id' => $pz_id,
            'dispatch_type' => 2,
            'dispatch_time' => time(),
        );
        Db::startTrans();
        try {
            $update_data = Db::name('server_order')->update($update);
            Db::commit();
            msg(0, '查询完成', $update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //完成陪诊导诊订单
    public function finishServerOrder()
    {
        $data = $this->request->param();
        $ServerOrderInfo = Db::name('server_order')
            ->where([
                'id' => $data['id'],
                'is_del' => 0
            ])->find();
        if (!$ServerOrderInfo) {
            msg(-1, '订单不存在', '');
        }
//        if ($ServerOrderInfo['status'] != 3) {
//            msg(-2, '订单状态不正确', '');
//        }
        if ($ServerOrderInfo['status'] == 4) {
            msg(0, '订单已完成', '');
        }

        Db::startTrans();
        try {
            $update_data = array(
                'status' => 4,
                'id' => $data['id'],
                'end_time' => time()
            );
            $pz_info = Db::name('pz')->find($ServerOrderInfo['pz_id']);
            Db::name('pz')->where('id','=',$ServerOrderInfo['pz_id'])
                ->setInc('order_num');
            addMoney($ServerOrderInfo,
                1,
                $pz_info['member_id'],
                $ServerOrderInfo['get_money'],
                '服务订单：' . $ServerOrderInfo['order_no'],
                1
            );
            //分销
            addFxMoney($ServerOrderInfo, 1);
            Db::name('server_order')->update($update_data);
            Db::name('server')->where([
                'id'=>$ServerOrderInfo['server_id']
            ])->setInc('sale_num');
            Db::commit();
            msg(0, '订单已完成', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

}